# ！/home/yang/PythonProjects python
# -*- coding: utf-8 -*-
# author:yang  time:19-7-25 下午4:29


year = int(input("请输入要查询的年份："))
month = int(input("请输入要查询的月份："))
month_31 = [1, 3, 5, 7, 8, 10, 12]
month_30 = [4, 6, 9, 11]

Sum = 0
# 为True表示闰年366


def is_run(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False


def month_choice(m):
    if m in month_31:
        return 31
    elif m in month_30:
        return 30
    else:
        if is_run(year):
            return 29
        else:
            return 28


for y in range(1900, year):
    if is_run(y):
        Sum += 366
    else:
        Sum += 365

for m in range(1, month):
    if m in month_31:
        Sum += 31
    elif m in month_30:
        Sum += 30
    else:
        if is_run(year):
            Sum += 29
        else:
            Sum += 28
print(Sum)

print("日\t一\t二\t三\t四\t五\t六")

count = 0
count += (Sum+1) % 7

for i in range(count):
    print("\t", end="")

for d_m in range(1, month_choice(month)+1):
    print(str(d_m)+"\t", end="")
    count += 1
    if count % 7 == 0:
        print()
